#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tBtnSelectItem_2->addAction(ui->actSelALL);
    ui->tBtnSelectItem_2->addAction(ui->actSelNone);
    ui->tBtnSelectItem_2->addAction(ui->actSelInvs);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actListIni_triggered()
{
    ui->listWidget_2->setSelectionMode(QAbstractItemView::ExtendedSelection);
    ui->listWidget_2->clear();
    for(int i = 0; i < 20; i++)
    {
        QListWidgetItem* item = new QListWidgetItem(QString("Item%1").arg(i));
        QIcon icon("F:\\Code\\Qt\\QListWidgetTest4_11\\QListWidgetTest\\icons\\check2.ico");
        item->setIcon(icon);
        item->setCheckState(Qt::Checked);
        if(ui->chkBoxListEditable_2->isChecked())
        {
            item->setFlags(item->flags() | Qt::ItemIsEditable);
        }
        ui->listWidget_2->addItem(item);
    }
}

void MainWindow::on_actListClear_triggered()
{
    ui->listWidget_2->clear();
}

void MainWindow::on_actListInsert_triggered()
{
    QListWidgetItem* item = new QListWidgetItem("InsertItem");
    QIcon icon("F:\\Code\\Qt\\QListWidgetTest4_11\\QListWidgetTest\\icons\\check2.ico");
    item->setIcon(icon);
    item->setCheckState(Qt::Checked);
    ui->listWidget_2->insertItem(0, item);
}

void MainWindow::on_actListAppend_triggered()
{
    QListWidgetItem* item = new QListWidgetItem("AppendItem");
    QIcon icon("F:\\Code\\Qt\\QListWidgetTest4_11\\QListWidgetTest\\icons\\check2.ico");
    item->setIcon(icon);
    item->setCheckState(Qt::Checked);
    ui->listWidget_2->addItem(item);
}

void MainWindow::on_actListDelete_triggered()
{
    QList<QListWidgetItem*> selectedItems = ui->listWidget_2->selectedItems();
    if (!selectedItems.isEmpty()) {  // 如果有选中的 item
        foreach (QListWidgetItem* item, selectedItems)
        {
            // 方法1：使用 takeItem 和 delete
            int row = ui->listWidget_2->row(item);
            delete ui->listWidget_2->takeItem(row);

            // 方法2：直接删除 item (Qt 会自动处理)
            // delete item;
        }
    }
}

void MainWindow::on_actSelALL_triggered()
{
    for(int i = 0; i < ui->listWidget_2->count(); ++i)
    {
        QListWidgetItem* item = ui->listWidget_2->item(i);
        item->setCheckState(Qt::Checked);
    }
}

void MainWindow::on_actSelNone_triggered()
{
    for(int i = 0; i < ui->listWidget_2->count(); ++i)
    {
        QListWidgetItem* item = ui->listWidget_2->item(i);
        item->setCheckState(Qt::Unchecked);
    }
}

void MainWindow::on_actSelInvs_triggered()
{
    for(int i = 0; i < ui->listWidget_2->count(); ++i)
    {
        QListWidgetItem* item = ui->listWidget_2->item(i);
        if(item->checkState() == Qt::Checked)
            item->setCheckState(Qt::Unchecked);
        else
            item->setCheckState(Qt::Checked);
    }
}

void MainWindow::on_tBtnSelALL_2_clicked()
{
    ui->actSelALL->trigger();
}

void MainWindow::on_tBtnSelNone_2_clicked()
{
    ui->actSelNone->trigger();
}

void MainWindow::on_tBtnSelInvs_2_clicked()
{
    ui->actSelInvs->trigger();
}

void MainWindow::on_listWidget_2_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    Q_UNUSED(previous);
    ui->editCutItemText_2->setText(current->text());
}

void MainWindow::on_tBtnSortAsc_2_clicked()
{
    if(ui->chkBoxSorting_2->isChecked())
    {
        ui->listWidget_2->sortItems(Qt::AscendingOrder);
    }
}

void MainWindow::on_tBtnSortDes_2_clicked()
{
    if(ui->chkBoxSorting_2->isChecked())
    {
        ui->listWidget_2->sortItems(Qt::DescendingOrder);
    }
}

void MainWindow::on_listWidget_2_currentRowChanged(int currentRow)
{
    ui->plainTextEdit_2->appendPlainText(QString("on_listWidget_2_currentRowChanged 信号触发 current row: %1").arg(currentRow));
}


void MainWindow::on_listWidget_2_itemChanged(QListWidgetItem *item)
{
     ui->plainTextEdit_2->appendPlainText("on_listWidget_2_itemChanged item : " + item->text());
}

void MainWindow::on_tBtnClearText_2_clicked()
{
    ui->plainTextEdit_2->clear();
}

void MainWindow::on_tBtnAddLine_2_clicked()
{
    ui->plainTextEdit_2->appendPlainText("");
}

